package com.example.volunteer.controller;

import com.example.volunteer.model.Admin;
import com.example.volunteer.service.AdminService;
import com.example.volunteer.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/admins")
public class AdminController {

    private final AdminService adminService;

    @Autowired
    public AdminController(AdminService adminService) {
        this.adminService = adminService;
    }

    @PostMapping("/create")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<String> createAdmin(@RequestBody Admin admin) {
        adminService.createAdmin(admin);
        return ResponseEntity.ok("管理员创建成功");
    }

    @GetMapping("/id")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<Admin> getAdminById(@RequestParam String adminId) {
        return ResponseEntity.ok(adminService.findById(adminId));
    }


    @PutMapping("/update")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<String> updateAdmin(@RequestBody Admin admin) {
        adminService.update(admin);
        return ResponseEntity.ok("管理员修改成功");
    }

    @DeleteMapping("/delete")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<String> deleteAdmin(@RequestBody Admin admin) {
        adminService.delete(admin);
        return ResponseEntity.ok("管理员删除成功");
    }

    @GetMapping()
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<PageResult<Admin>> getAdminsWithPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return ResponseEntity.ok(adminService.findAdminsWithPage(pageNum, pageSize));
    }

    @GetMapping("/name")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<PageResult<Admin>> getByNameWithPage(
            @RequestParam String name,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return ResponseEntity.ok(adminService.findByNameWithPage(name, pageNum, pageSize));
    }

    @GetMapping("/email")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<PageResult<Admin>> getByEmailWithPage(
            @RequestParam String email,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return ResponseEntity.ok(adminService.findByEmailWithPage(email, pageNum, pageSize));
    }

    @GetMapping("/contact")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<PageResult<Admin>> getByContactWithPage(
            @RequestParam String contact,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        return ResponseEntity.ok(adminService.findByContactWithPage(contact, pageNum, pageSize));
    }
}